package smlab.utils
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLStream;
	import flash.system.ApplicationDomain;
	
	/**
	 * 加载辅助类
	 * @author simon
	 * 
	 */
	public class LoadUtils
	{
		public function LoadUtils()
		{
		}
		
		/**
		 * 从某程序域中获取某个类
		 * @param app
		 * @param className
		 * @return 
		 * 
		 */
		public static function getClassFromApp(app:ApplicationDomain,className:String):Class{
			if(app && className && className!=""){
				return Class(app.getDefinition(className));
			}else{
				return null;
			}
		}
		
		/**
		 * 从某 Loader 中获取某个类
		 * @param loader
		 * @param className
		 * @return 
		 * 
		 */
		public static function getClassFromLoader(loader:Loader,className:String):Class{
			try {
                if(loader.contentLoaderInfo.applicationDomain.hasDefinition(className)){
                    var obj:Object=loader.contentLoaderInfo.applicationDomain.getDefinition(className)
                    return Class(obj);
                }else{
                    return null
                }
            } catch (e:Error) {
                trace("getClassFromLoader()::Error",e,loader,className)
            }
            return null;
		}
		
		/**
		 * 为 URLStream/URLLoader 添加/移除监听
		 * @param stream
		 * @param isAdd                是添加还是删除监听
		 * @param completeHandler
		 * @param httpStatusHandler
		 * @param ioErrorHandler
		 * @param openHandler
		 * @param progressHandler
		 * @param securityErrorHandler
		 * 
		 */
		public static function addListener_URLStream_URLLoader(stream:URLStream=null,
		urlLoader:URLLoader=null,
		isAdd:Boolean=true,
		completeHandler:Function=null,
		httpStatusHandler:Function=null,
		ioErrorHandler:Function=null,
		openHandler:Function=null,
		progressHandler:Function=null,
		securityErrorHandler:Function=null):void
		{
			if(urlLoader==null && stream==null)return;
			var d:IEventDispatcher=urlLoader ? urlLoader:stream;
			
			if(isAdd){
				if(completeHandler!=null)d.addEventListener(Event.COMPLETE, completeHandler);
	            if(httpStatusHandler!=null)d.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
	            if(ioErrorHandler!=null)d.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	            if(openHandler!=null)d.addEventListener(Event.OPEN, openHandler);
	            if(progressHandler!=null)d.addEventListener(ProgressEvent.PROGRESS, progressHandler);
	            if(securityErrorHandler!=null)d.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			}else{
				if(completeHandler!=null)d.removeEventListener(Event.COMPLETE, completeHandler);
	            if(httpStatusHandler!=null)d.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
	            if(ioErrorHandler!=null)d.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	            if(openHandler!=null)d.removeEventListener(Event.OPEN, openHandler);
	            if(progressHandler!=null)d.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
	            if(securityErrorHandler!=null)d.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			}
            
		}
		
		/**
		 * 为 Loader 添加/移除监听
		 * @param l
		 * @param isAdd                是添加还是删除监听
		 * @param completeHandler
		 * @param httpStatusHandler
		 * @param initHandler
		 * @param ioErrorHandler
		 * @param openHandler
		 * @param progressHandler
		 * @param unLoadHandler
		 * @param securityErrorHandler
		 * 
		 */
		public static function addListener_Loader(l:LoaderInfo,isAdd:Boolean=true,
		completeHandler:Function=null,
		httpStatusHandler:Function=null,
		initHandler:Function=null,
		ioErrorHandler:Function=null,
		openHandler:Function=null,
		progressHandler:Function=null,
		unLoadHandler:Function=null,
		securityErrorHandler:Function=null):void
		{
			if(l==null)return;
			
			if(isAdd){
				if(completeHandler!=null)l.addEventListener(Event.COMPLETE, completeHandler);
		        if(httpStatusHandler!=null)l.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
		        if(initHandler!=null)l.addEventListener(Event.INIT, initHandler);
		        if(ioErrorHandler!=null)l.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		        if(openHandler!=null)l.addEventListener(Event.OPEN, openHandler);
		        if(progressHandler!=null)l.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		        if(unLoadHandler!=null)l.addEventListener(Event.UNLOAD, unLoadHandler);
	            if(securityErrorHandler!=null)l.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			}else{
				if(completeHandler!=null)l.removeEventListener(Event.COMPLETE, completeHandler);
				if(httpStatusHandler!=null)l.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
				if(initHandler!=null)l.removeEventListener(Event.INIT, initHandler);
	            if(ioErrorHandler!=null)l.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	            if(openHandler!=null)l.removeEventListener(Event.OPEN, openHandler);
	            if(progressHandler!=null)l.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
	            if(unLoadHandler!=null)l.removeEventListener(Event.UNLOAD, unLoadHandler);
	            if(securityErrorHandler!=null)l.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			}
		}
	}
}